package spoon.service;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import spoon.core.domain.Game;
import spoon.core.exception.ConnectionException;
import spoon.core.service.HttpService;

@Service
public class TenbetHttpServiceImpl implements TenbetHttpService {
	
	@Autowired
	private HttpService httpService;
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 
	 * 
	 * @param itemId
	 * @return
	 */
	@Override
	public List<Game> getLeagueList(String itemId) {
		
		HttpURLConnection connection = httpService.getUrlConnection("http://www.10bet.com/pagemethods.aspx/getBranchFilters");
		connection.setRequestProperty("RequestTarget", "AJAXService");
		connection.setRequestProperty("Referer", "http://www.10bet.com/sports/soccer/?utm_source=shp&utm_medium=sleft&utm_campaign=soccer");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.setRequestProperty("Host", "www.10bet.com");
		connection.setRequestProperty("Connection", "Keep-Alive");
		
		try (OutputStream os = connection.getOutputStream();) {
			os.write(("BranchID=" + itemId).getBytes("UTF-8"));
			os.flush();
		} catch (IOException e) {
			throw new ConnectionException(connection.toString(), e);
		}

		String source = httpService.getSource(connection);
		source = source.replaceAll("]]]", "]#");
		
		Pattern p = Pattern.compile("\\[([0-9]+),\\\"([^\\\"]+)\\\",[0-9]+,[0-9]+,[0-9]+,\\[(\\[[^#]+)#");
		Matcher m = p.matcher(source);
		
		List<Game> leagueList = new ArrayList<Game>();
		
		while (m.find()) {
			Game league = new Game();
			league.setLeagueName(m.group(2));
			league.setLink(m.group(1));
			league.setGameId(m.group(1));
			league.setBetType(m.group(3)); // 베팅타입 저장 (나중에 불러 오자)
			leagueList.add(league);
		}
		
		return leagueList;
	}

	@Override
	public String getGameList(String itemId, String leagueId) {
		HttpURLConnection connection = httpService.getUrlConnection("http://www.10bet.com/pagemethods.aspx/GetLeaguesContent");
		connection.setRequestProperty("RequestTarget", "AJAXService");
		connection.setRequestProperty("Referer", "http://www.10bet.com/sports/soccer/?utm_source=shp&utm_medium=sleft&utm_campaign=soccer");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.setRequestProperty("Host", "www.10bet.com");
		connection.setRequestProperty("Connection", "Keep-Alive");
		
		try (OutputStream os = connection.getOutputStream();) {
			os.write(("BranchID=" + itemId + "&LeaguesCollection=" + leagueId).getBytes("UTF-8"));
			os.flush();
		} catch (IOException e) {
			throw new ConnectionException(connection.toString(), e);
		}

		String source = httpService.getSource(connection);
		
		return source;
	}

	@Override
	public String getBettingList(String param) {
		HttpURLConnection connection = httpService.getUrlConnection("http://www.10bet.com/pagemethods.aspx/UpdateEvents");
		connection.setRequestProperty("RequestTarget", "AJAXService");
		connection.setRequestProperty("Referer", "http://www.10bet.com/sports/soccer/?utm_source=shp&utm_medium=sleft&utm_campaign=soccer");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		connection.setRequestProperty("Host", "www.10bet.com");
		connection.setRequestProperty("Connection", "Keep-Alive");
		
		try (OutputStream os = connection.getOutputStream();) {
			os.write(param.getBytes("UTF-8"));
			os.flush();
		} catch (IOException e) {
			throw new ConnectionException(connection.toString(), e);
		}

		String source = httpService.getSource(connection);
		
		return source;
	}
	

}
